<template>
    <div>
        <h1>添加</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">名称</td>
                    <td><input type="text" v-model="info.Name"></td>
                </tr>
                <tr>
                    <td align="right">公告内容</td>
                    <td><textarea v-model="info.NeiRong"></textarea></td>
                </tr>

                <tr>
                    <td align="right">图片</td>
                    <td><input type="file" @change="Imgs">
                        <img :src="info.Img" width="100" height="100" v-if="info.Img != ''">
                    </td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td><input type="radio" name="str" v-model="info.State" :value="true">启用
                        <input type="radio" name="str" v-model="info.State" :value="false">禁用
                    </td>
                </tr>
                <tr>
                    <td align="right">类型</td>
                    <td>
                        <select v-model="info.LX">
                            <option value="">请选择</option>
                            <option value="服务协议">服务协议</option>
                            <option value="修车">修车</option>
                            <option value="宠物">宠物</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router';
const rouder = useRouter();
let info: any = reactive({
    Name: '',
    NeiRong: '',
    Img: '',
    State: '',
    LX: '',

})

const Imgs = (e: any) => {
    let obj = e.target.files[0]
    let f = new FormData();
    f.append('file', obj)
    axios({
        url: '/api/Img/Imgs',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            info.Img = res.data
        })
        .catch((err) => {
            console.log(err);
        })
}

const ok = () => {
    axios({
        url: '/api/GongGao/Add',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert("名称重复")
                return;
            }
            if (res.data > 0) {
                alert("添加成功")
                return;
            }
            else {
                alert("添加失败")
                return;
            }

        })
}

</script>

<style scoped></style>